package com.fowo.api.common.validator;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

@Slf4j
public class PhoneValidator implements ConstraintValidator<Phone, String> {

    private static final Pattern phonePattern = Pattern.compile("1\\d{10}");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(StringUtils.hasText(value)) {
            return phonePattern.matcher(value).matches();
        }
        return true;
    }
}
